<template>
  <app-dialog class="user-detail-dialog" v-model="show" width="600px" title="会员授权" @open="handleOpen">
    <el-form ref="formRef" :model="formData" :inline="true" label-width="100px" label-position="right"
      style="width: 70%; margin: 0 auto">
      <el-form-item label="选择组织">
        <el-select v-model="formData.plant_group_ids" multiple style="width: 300px">
          <el-option v-for="(item, index) in plantgrouplist" :key="index" :value="item.id" :label="item.pg_name" />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button type="primary" @click="submit()">确定</el-button>
    </template>
  </app-dialog>
</template>

<script setup>
import { http } from '@/utils/http'
import { ElMessage } from 'element-plus'
const show = defineModel({ type: Boolean })
const emits = defineEmits(['change'])
const props = defineProps({
  data: Object,
})

const plantgrouplist = ref([])
const formData = ref({
  plant_group_ids: [],
})

const getplantgrouplist = () => {
  http.get('/accredit/plant_group_list?page=1&page_size=999').then(res => {
    plantgrouplist.value = res.data.list
  })
}

const handleOpen = () => {
  formData.value.plant_group_ids = []
  if (props.data.mem_accredits.length > 0) {
    props.data.mem_accredits.forEach(item => {
      formData.value.plant_group_ids.push(item.plant_group.id)
    })
  }
  getplantgrouplist()
}

const submit = () => {
  console.log(formData.value.plant_group_ids)

  // if (formData.value.plant_group_ids.length > 0) {
    let body = {
      plant_group_ids: formData.value.plant_group_ids,
      member_id: props.data.id,
    }
    console.log(body);

    http.post('/accredit/accredit_member', body).then(res => {
      ElMessage.success('操作成功')
      show.value = false
    })
  // } else {
  //   let body = {
  //     id: props.data.id,
  //   }

  //   http.delete(`/accredit/delete_accredit_member?id=${props.data.id}`, body).then(res => {
  //     ElMessage.success('操作成功')
  //     show.value = false
  //   })
  // }
}
</script>

<style lang="scss" scoped></style>